package com.ruoyi.api.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.bo.HcxMerchantJoinBo;
import com.ruoyi.business.app.vo.AppJoinPhotoVo;
import com.ruoyi.business.app.vo.AppMerchantDetailVo;
import com.ruoyi.business.app.vo.AppMerchantPhotoVo;
import com.ruoyi.business.app.vo.AppMerchantTypeVo;
import com.ruoyi.business.domain.HcxJoinPhoto;
import com.ruoyi.business.domain.HcxMerchant;
import com.ruoyi.business.domain.HcxMerchantPhoto;
import com.ruoyi.business.service.IHcxJoinPhotoService;
import com.ruoyi.business.service.IHcxMerchantPhotoService;
import com.ruoyi.business.service.IHcxMerchantService;
import com.ruoyi.business.service.IHcxMerchantTypeService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "我的--->门店加盟")
@AllArgsConstructor
@RestController
@RequestMapping("/api/my")
public class MyJoinController extends BaseController {

    private final IHcxMerchantService iHcxMerchantService;
    private final IHcxMerchantPhotoService iHcxMerchantPhotoService;
    private final IHcxJoinPhotoService iHcxJoinPhotoService;

    private final IHcxMerchantTypeService iHcxMerchantTypeService;


    @ApiOperation("商家入驻的时候宣传图")
    @GetMapping("/joinphoto")
    @Login
    public AjaxResult<AppJoinPhotoVo> joinphoto() {
        List<AppJoinPhotoVo> appJoinPhotoVos = iHcxJoinPhotoService.listVo(new LambdaQueryWrapper<HcxJoinPhoto>().orderByDesc(HcxJoinPhoto::getCreateTime), AppJoinPhotoVo.class);
        AssertUtils.isTrue(CollectionUtil.isNotEmpty(appJoinPhotoVos), "请先在后台上传宣传图!");
        return AjaxResult.success(appJoinPhotoVos.get(0));
    }



    @ApiOperation("商家信息(app入驻的时候调用 有可能之前提交过入驻申请没有通过)")
    @GetMapping("/joindetail")
    @Login
    public AjaxResult<AppMerchantDetailVo> joindetail() {
        AppMerchantDetailVo voOne = iHcxMerchantService.getVoOne(new LambdaQueryWrapper<HcxMerchant>().eq(HcxMerchant::getUserId, SecurityUtils.getUserId()), AppMerchantDetailVo.class);
        if(ObjectUtil.isNotEmpty(voOne)){
            List<AppMerchantPhotoVo> photoList = iHcxMerchantPhotoService.listVo(new LambdaQueryWrapper<HcxMerchantPhoto>().eq(HcxMerchantPhoto::getMerchantId, voOne.getId()), AppMerchantPhotoVo.class);
            voOne.setPhotos(photoList);
        }else{
            voOne=new AppMerchantDetailVo();
        }
        return AjaxResult.success(voOne);
    }


    @RepeatSubmit
    @ApiOperation("商家入驻")
    @PostMapping("/join")
    @Login
    public AjaxResult join(@Validated @RequestBody HcxMerchantJoinBo bo) {
        Boolean join = iHcxMerchantService.join(bo);
        return toAjax(join);
    }



   /* @Login
    @ApiOperation("获取商家分类列表")
    @GetMapping("/merchantTypes")
    public AjaxResult<List<TreeSelect>> merchantTypes() {
        List<HcxMerchantType> list = iHcxMerchantTypeService.list(Wrappers.emptyWrapper());
        return AjaxResult.success(iHcxMerchantTypeService.buildMerchantTypeTreeSelect(list));
    }*/

    @Login
    @ApiOperation("获取商家分类列表")
    @GetMapping("/merchantTypes")
    public AjaxResult<List<AppMerchantTypeVo>> merchantTypes() {
        List<AppMerchantTypeVo> list = iHcxMerchantTypeService.listVo(Wrappers.emptyWrapper(),AppMerchantTypeVo.class);
        return AjaxResult.success(list);
    }

}
